/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dk.redia.lib.utils.Base64;
import dk.redia.libraryapp.model.Externals;
import dk.redia.libraryapp.model.ExternalsUrl;
import dk.redia.libraryapp.activityutils.LibraryAppActivity;



public class AboutTheAuthorActivity extends LibraryAppActivity {

	public static final String EXTRAS_KEY_EXTERNALS = "externals";

	private WebView bodyWebView;
	private Externals externals;
	private LayoutInflater inflater;
	private LinearLayout urlItemsLinearLayout;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.abouttheauthoractivity);
	    
	    inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    externals = (Externals)getIntent().getParcelableExtra(EXTRAS_KEY_EXTERNALS);
	    
	    bodyWebView = (WebView)findViewById(R.id.aboutTheAuthorActivityBodyWebView);
	    
	    String html = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><html>" +
	    		"<head>" +
	    		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" + 
	    		"<style type=\"text/css\">" + 
	    		"#bluebook { font-size:14px; }" +
    			"body { color:#ffffff; font-family:helvetica neue,arial,sans-serif;font-size:12px }" + 
	    		"</style>" + 
	    		"</head>" +
	    		"<body><h1>" + externals.getAboutAuthorName() + "</h1><br>" + 
	    		Externals.rewriteAuthorDescription(externals.getAboutAuthorDescription()) + 
	    		"</body>" +
	    		"</html>";
	    bodyWebView.setBackgroundColor(getResources().getColor(R.color.globalBackgroundColor));
	    bodyWebView.loadData(Base64.encodeToString(html.getBytes(), Base64.DEFAULT), "text/html; charset=utf-8", "base64");
	
	    urlItemsLinearLayout = (LinearLayout)findViewById(R.id.aboutTheAuthorActivityLinkItemsLinearLayout);
	    
	    for (ExternalsUrl url : externals.getAboutAuthorUrls()) {
	    	RelativeLayout item = (RelativeLayout)inflater.inflate(R.layout.abouttheauthorlinkitem, null);
	    	RelativeLayout clickable = (RelativeLayout)item.findViewById(R.id.aboutTheAuthorActivityLinkItemRelativeLayout);
	    	TextView textView = (TextView)item.findViewById(R.id.aboutTheAuthorActivityLinkItemTextView);
	    	textView.setText(url.title);
	    	textView.setTag(url.url);
	    	clickable.setTag(url.url);
	    	item.setTag(url.url);
	    	urlItemsLinearLayout.addView(item);
	    }
	}
	
	public void onUrlItemClicked(View v) {
		String url = (String)v.getTag();
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}
}
